#自定义类
#如何定义：class Name： 名字一般首字母大写
#静态方法：@staticmethod，不操作实例属性，无法访问实例属性
#类方法：@classmethod   会有一个cls属性，代指类对象:   使用场景，父类的非实例方法需要访问子类的类属性
#初始化方法：__init__(self):
#实例化对象的时候，会先在内存中创建该类的实例对象，然后立即调用类的__init__方法，调用的时候，就将刚才创建的对象传递给self参数
#实例属性和类属性同名：通过类访问类属性，通过实例访问实例属性
#继承 class zilei(fulei):
#子类有__init__方法时，如果不调用父类的方法，解释器是不会主动调用的 必须写上 Fulei.__init__(self,para1,para2),或者用super().__init__(para1,para2)
#super() 这样写，不用加上self，不用写死父类的名字，代码可维护性好
#


class bin:

    def __init__(self,name):
        self.name = name

    def huifu(self,huifuliang):
        if self.hp + huifuliang >= self.max_hp:
            self.hp = self.max_hp
        else:
            self.hp = self.hp + huifuliang
    @classmethod
    def bin_info(cls):
        print(f'我是{cls.shuxing}，我的价格是{cls.price}')

class arrow_bin(bin):
    price = 100
    max_hp = 100
    shuxing = '弓箭手'

    def __init__(self):
        self.hp = arrow_bin.max_hp
        self.alive = True

    
    
    def attack(self,target):
        if target == '狼妖':
            self.hp -= 80
        if target == '鹰妖':
            self.hp -= 20
        if self.hp <= 0:
            self.alive = False

    # def huifu(self,huifuliang):
    #     if self.hp + huifuliang >= arrow_bin.max_hp:
    #         self.hp = arrow_bin.max_hp
    #     else:
    #         self.hp = self.hp + huifuliang

class axe_bin(bin):
    price = 120
    max_hp = 120
    shuxing = '斧头兵'

    def __init__(self):
        self.hp = arrow_bin.max_hp
        self.alive = True
    
    
    def attack(self,target):
        if target == '狼妖':
            self.hp -= 20
        if target == '鹰妖':
            self.hp -= 80
        if self.hp <= 0:
            self.alive = False
    
    # def huifu(self,huifuliang):
    #     if self.hp + huifuliang >= arrow_bin.max_hp:
    #         self.hp = arrow_bin.max_hp
    #     else:
    #         self.hp = self.hp + huifuliang

class user:
    def __init__(self,coin):
        self.coin = coin
        self.shouxia = {}

    def buy(self,bing):
        self.coin -= bing.price
        name=input('请输入这个兵的名字:')
        self.shouxia[name]=bing
    
    def huifu(self,coin,bing_name):
        self.coin -= coin
        self.shouxia[bing_name].huifu(coin)

    def siwang(self,bing_name):
        if not self.shouxia[bing_name].alive:
            self.shouxia.pop(bing_name)
    
    def chakan_bing(self):
        for k,v in self.shouxia.items():
            print(f'名称：{k:>10}，属性：{v.shuxing},血量:{v.hp}')

    def chakan_coin(self):
        print(self.coin)



def liucheng():
    u = user(1000)

    #购买手下
    while True:
        caozuo=input('是否需要购买战士？购买弓箭手输入1，购买斧头兵输入2，查看已购买的输入0，查看剩余金币数量请输入8，退出购买环节输入9:')
        if caozuo == '1':

            u.buy(arrow_bin())
        elif caozuo == '2':

            u.buy(axe_bin())
        elif caozuo == '0':
            
            u.chakan_bing()
       
        elif caozuo == '8':
            u.chakan_coin()

        elif caozuo == '9':
            break

        else:
            print('操作不合法，请重试！')
            continue
    
    print('进入战斗！')
    yaoguai = ['鹰妖','鹰妖','狼妖','狼妖','狼妖']
    print(f'你将要面对的妖怪是：{yaoguai}')

    for i in yaoguai:
        print(f'你要面对的是：{i}')
        
        u.chakan_bing()

        while True:

            if u.shouxia == {}:
                print('你已经没有手下了，战斗失败！')
                return 0

            name = input('输入你要出战的战士名称：')
            if name in u.shouxia:
                u.shouxia[name].attack(i)
            else:
                print('输入有误，请重试')
                continue
            #活着
            if u.shouxia[name].alive:
                print('战斗胜利')
                
                u.chakan_bing()
                #胜利是否需要补给
                u.chakan_coin()
                caozuo1 = input('是否需要恢复生命值，是输入0，开始下一场战斗输入1:')
                if caozuo1 == "0":
                    liang = input(f'当前生命值为：{u.shouxia[name].hp},请输入需要恢复多少生命值：')
                    u.huifu(int(liang),name)
                    break
                elif caozuo1 == '1':
                    break

            else:
                #死亡换人
                print('战斗失败！')
                u.siwang(name)
                continue

    print('你已经通过了考验！')
    print(f'你剩余的金币为：{u.chakan_coin()}')
    



# liucheng()

c = axe_bin()
c.bin_info()
d = arrow_bin()
d.bin_info()